import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:my_flutter_test/core/base/BaseFulWidget.dart';
import 'package:my_flutter_test/core/base/BaseLogic.dart';
import 'package:my_flutter_test/core/widget/MyAppBar.dart';
import 'package:my_flutter_test/zzc/FlutterMethod.dart';
import 'package:my_flutter_test/zzc/NetManager.dart';

import 'getx/demo2_page.dart';

//逻辑都写在这里
class SimpleLogic extends BaseLogic {
  var title = "标题".obs;
  var data = "ddddd".obs;
  var platformVersion = "默认".obs;

  void registerNativeCall() {
    FlutterMethod(this.context);
  }

  Future<void> post() async {
    Object response;
    try {
      response = await NetManager.post({"cmd": "109", "platform": "android"}) ??
          "call error";
    } on PlatformException catch (e) {
      print(e.code);
      response = "${e.code} ${e.message}";
    }
    var test = NetManager.xxxx("xxxxx");
    print("test $test");
    platformVersion.value = "ddddd" + response.toString(); // "测试顶顶顶顶";
  }

  void jumpPage() {
    Get.to(Demo2());

  }
}


//页面相关
class SimplePage extends BaseFulWidget<SimpleLogic> {
  TextEditingController _controller = TextEditingController();
  String? finalData;

  SimplePage({Key? key, required String data, String? finalData})
      : super(key: key) {
    this.finalData = finalData;
    // logic.data.value = data;
  }

  @override
  SimpleLogic buildLogic() {
    //单界面
    return Get.put(SimpleLogic());
    // 如果需要跨组件使用，需要put到Get上 or
    // return  Get.put(SimpleLogic());
  }

  @override
  Widget build(BuildContext context) {
    // 注册FlutterMethod，等待Native的调用
    // logic.registerNativeCall();
    return Scaffold(
      appBar: MyAppBar.buildNormalAppBar(),
      body: Center(
        child: ListView(
          physics: const AlwaysScrollableScrollPhysics(),
          children: [
            const SizedBox(height: 100),
            Align(
                alignment: Alignment.center,
                child: Obx(() {
                  return Text("${logic.data} flutter page",
                      style: TextStyle(fontSize: 26));
                })
            ),
            const SizedBox(height: 50),
            Align(
                alignment: Alignment.center,
                child: Obx(() =>
                    Text(
                        " data:${logic.data}  this.finalData ${this
                            .finalData}",
                        style: TextStyle(fontSize: 26)))),

            const SizedBox(height: 50),
            Obx(() => Text('Running on: ${logic.platformVersion}\n')),
            Padding(
                padding: const EdgeInsets.all(8.0),
                child: CupertinoTextField(
                  controller: _controller,
                  padding: EdgeInsets.symmetric(vertical: 16, horizontal: 20),
                  placeholder: 'data to return previous page',
                  decoration: BoxDecoration(color: Colors.amber),
                )),
            const SizedBox(height: 10),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: CupertinoButton.filled(
                padding: EdgeInsets.symmetric(vertical: 20, horizontal: 30),
                onPressed: () {
                  Get.back();
                },
                child: Text('测试返回',
                    style: TextStyle(
                        fontSize: 20,
                        color: Colors.white,
                        fontWeight: FontWeight.w500)),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: CupertinoButton.filled(
                padding: EdgeInsets.symmetric(vertical: 20, horizontal: 30),
                onPressed: () {
                  logic.jumpPage();
                },
                child: Text('打开本地页面',
                    style: TextStyle(
                        fontSize: 20,
                        color: Colors.white,
                        fontWeight: FontWeight.w500)),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: CupertinoButton.filled(
                padding: EdgeInsets.symmetric(vertical: 0, horizontal: 30),
                onPressed: () {
                  logic.data.value = "测试数据变更";
                  logic.post();
                },
                child: Text('网络请求',
                    style: TextStyle(
                        fontSize: 20,
                        color: Colors.white,
                        fontWeight: FontWeight.w500)),
              ),
            ),
          ],
        ),
      ),
    );
  }


}
